home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 076-100 / disk_098 / thai / do_gadget.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  9KB  |  393 lines

  1.  
  2. #include "quiz.h"
  3.  
  4. extern LONG        AutoRequest ();
  5.  
  6. #define REQ_WIDTH    300
  7. #define REQ_HEIGHT    50
  8.  
  9. struct IntuiText yes_text = {
  10.     FP , BP , COMPLEMENT|JAM1 , 5 , 3 , NULL , (UBYTE*) "Yes" , NULL
  11. };
  12.  
  13. struct IntuiText no_text = {
  14.     FP , BP , COMPLEMENT|JAM1 , 5 , 3 , NULL , (UBYTE*) "No" , NULL
  15. };
  16.  
  17. struct IntuiText ok_text = {
  18.     FP , BP , COMPLEMENT|JAM1 , 5 , 3 , NULL , (UBYTE*) "OK" , NULL
  19. };
  20.  
  21. char body_buf[ 100 ];
  22. struct IntuiText body_text = {
  23.     FP , BP , COMPLEMENT|JAM1 , 10 , 7 , NULL , (UBYTE*) &body_buf[0] , NULL
  24. };
  25.  
  26.  
  27. do_gadget ( gadget )
  28. struct Gadget *gadget;
  29. {
  30.     char *p;
  31.     char *word;
  32.     char *trans;
  33.     char *error_at;
  34.     char *sentence;
  35.     int i;
  36.     struct thai_phrase *phrase;
  37.  
  38.     if ( isthai ( (int)gadget->GadgetID ) ) {
  39.  
  40.         switch ( entry ) {
  41.  
  42.         case TW_ENTRY :
  43.             edit_thai ( screen_word.thai , TW_ENTRY , (char)gadget->GadgetID );
  44.             break;
  45.  
  46.         case TS_ENTRY :
  47.             edit_thai ( screen_sentence.thai , TS_ENTRY , (char)gadget->GadgetID );
  48.             break;
  49.  
  50.         default :
  51.             /* off_thai_keyboard (); */
  52.             break;
  53.         }
  54.     }
  55.     else {
  56.  
  57.         switch ( gadget->GadgetID ) {
  58.  
  59.         case CHOOSE_SENTENCE :
  60.             random_sentence ();
  61.             scanning = CHOOSE_SENTENCE;
  62.             wordptr = chosen_sentence->thai;
  63.             reset_next_prev ();
  64.             screen_sentence.thai[0] = '\0';
  65.             screen_sentence.phonetic[0] = '\0';
  66.             screen_sentence.english[0] = '\0';
  67.             show_sentence = 0;
  68.             refresh_screen ();
  69.             break;
  70.  
  71.         case SPEAK_SENTENCE :
  72.             if ( screen_sentence.phonetic[0] != '\0' )
  73.                 speak ( screen_sentence.phonetic );
  74.             else
  75.                 speak ( chosen_sentence->phonetic );
  76.             break;
  77.  
  78.         case THAI_SENTENCE :
  79.             show_sentence |= SHOW_THAI;
  80.             strcpy ( screen_sentence.thai , chosen_sentence->thai );
  81.             refresh_gadget ( TS_ENTRY );
  82.             break;
  83.  
  84.         case PHONETIC_SENTENCE :
  85.             show_sentence |= SHOW_PHONETIC;
  86.             strcpy ( screen_sentence.phonetic , chosen_sentence->phonetic );
  87.             refresh_gadget ( PS_ENTRY );
  88.             break;
  89.  
  90.         case ENGLISH_SENTENCE :
  91.             show_sentence |= SHOW_ENGLISH;
  92.             strcpy ( screen_sentence.english , chosen_sentence->english );
  93.             refresh_gadget ( ES_ENTRY );
  94.             break;
  95.  
  96.         case RIGHT_SENTENCE :
  97.             file_changed = TRUE;
  98.             chosen_sentence->right++;
  99.             break;
  100.  
  101.         case WRONG_SENTENCE :
  102.             file_changed = TRUE;
  103.             chosen_sentence->wrong++;
  104.             break;
  105.  
  106.         case ADD_SENTENCE :
  107.             if ( thai_search ( &sentence_head , screen_sentence.thai ,
  108.                 NEXT_ENTRY ) != NULL ) {
  109.  
  110.                 strcpy ( body_buf , "Sentence exists - add anyway?" );
  111.                 if( !AutoRequest ( window , &body_text , &yes_text , &no_text ,
  112.                     (LONG)0 , (LONG)0 , (LONG)REQ_WIDTH , (LONG)REQ_HEIGHT ) )
  113.  
  114.                     break;
  115.             }
  116.  
  117.             file_changed = TRUE;
  118.             if ( !add_phrase ( &sentence_head , &screen_sentence ) ) {
  119.                 strcpy ( body_buf , "Add failed (out of memory?)" );
  120.                 AutoRequest ( window , &body_text , (char*)NULL , &ok_text ,
  121.                     (LONG)0 , (LONG)0 , (LONG)REQ_WIDTH , (LONG)REQ_HEIGHT );
  122.             }
  123.             clr_sentence ();
  124.             break;
  125.  
  126.         case DEL_SENTENCE :
  127.             file_changed = TRUE;
  128.             del_phrase ( &sentence_head , chosen_sentence );
  129.             clr_sentence ();
  130.             break;
  131.  
  132.         case REPLACE_SENTENCE :
  133.             file_changed = TRUE;
  134.             replace_phrase ( chosen_sentence , &screen_sentence );
  135.             break;
  136.  
  137.         case CHOOSE_WORD :
  138.             random_word ();
  139.             scanning = -1;
  140.             reset_next_prev ();
  141.             screen_word.thai[0] = '\0';
  142.             screen_word.phonetic[0] = '\0';
  143.             screen_word.english[0] = '\0';
  144.             show_word = 0;
  145.             refresh_screen ();
  146.             break;
  147.  
  148.         case SPEAK_WORD :
  149.             if ( screen_word.phonetic[0] != '\0' )
  150.                 speak ( screen_word.phonetic );
  151.             else
  152.                 speak ( chosen_word->phonetic );
  153.             break;
  154.  
  155.         case THAI_WORD :
  156.             show_word |= SHOW_THAI;
  157.             strcpy ( screen_word.thai , chosen_word->thai );
  158.             refresh_gadget ( TW_ENTRY );
  159.             break;
  160.  
  161.         case PHONETIC_WORD :
  162.             show_word |= SHOW_PHONETIC;
  163.             strcpy ( screen_word.phonetic , chosen_word->phonetic );
  164.             refresh_gadget ( PW_ENTRY );
  165.             break;
  166.  
  167.         case ENGLISH_WORD :
  168.             show_word |= SHOW_ENGLISH;
  169.             strcpy ( screen_word.english , chosen_word->english );
  170.             refresh_gadget ( EW_ENTRY );
  171.             break;
  172.  
  173.         case RIGHT_WORD :
  174.             file_changed = TRUE;
  175.             chosen_word->right++;
  176.             break;
  177.  
  178.         case WRONG_WORD :
  179.             file_changed = TRUE;
  180.             chosen_word->wrong++;
  181.             break;
  182.  
  183.         case ADD_WORD :
  184.             if ( thai_search ( &word_head , screen_word.thai ,
  185.                 NEXT_ENTRY ) != NULL ) {
  186.  
  187.                 strcpy ( body_buf , "Word exists - add anyway?" );
  188.                 if( !AutoRequest ( window , &body_text , &yes_text , &no_text ,
  189.                     (LONG)0 , (LONG)0 , (LONG)REQ_WIDTH , (LONG)REQ_HEIGHT ) )
  190.  
  191.                     break;
  192.             }
  193.  
  194.             file_changed = TRUE;
  195.             if ( !add_phrase ( &word_head , &screen_word ) ) {
  196.                 strcpy ( body_buf , "Add failed (out of memory?)" );
  197.                 AutoRequest ( window , &body_text , (char*)NULL , &ok_text ,
  198.                     (LONG)0 , (LONG)0 , (LONG)REQ_WIDTH , (LONG)REQ_HEIGHT );
  199.             }
  200.             clr_word ();
  201.             break;
  202.  
  203.         case DEL_WORD :
  204.             file_changed = TRUE;
  205.             del_phrase ( &word_head , chosen_word );
  206.             clr_word ();
  207.             break;
  208.  
  209.         case REPLACE_WORD :
  210.             file_changed = TRUE;
  211.             replace_phrase ( chosen_word , &screen_word );
  212.             break;
  213.  
  214.         case TS_SEARCH :
  215.             strcpy ( scan.thai , screen_sentence.thai );
  216.             phrase = thai_search ( &sentence_head , scan.thai , NEXT_ENTRY );
  217.             if ( phrase != NULL ) {
  218.                 show_sentence = SHOW_THAI | SHOW_PHONETIC | SHOW_ENGLISH;
  219.                 scanning = TS_SEARCH;
  220.                 new_sentence ( phrase );
  221.             }
  222.             else
  223.                 not_found ();
  224.             break;
  225.  
  226.         case PS_SEARCH :
  227.             strcpy ( scan.phonetic , screen_sentence.phonetic );
  228.             phrase = phonetic_search ( &sentence_head , scan.phonetic ,
  229.                 NEXT_ENTRY );
  230.             if ( phrase != NULL ) {
  231.                 show_sentence = SHOW_THAI | SHOW_PHONETIC | SHOW_ENGLISH;
  232.                 scanning = PS_SEARCH;
  233.                 new_sentence ( phrase );
  234.             }
  235.             else
  236.                 not_found ();
  237.             break;
  238.  
  239.         case ES_SEARCH :
  240.             strcpy ( scan.english , screen_sentence.english );
  241.             phrase = english_search ( &sentence_head , scan.english ,
  242.                 NEXT_ENTRY );
  243.             if ( phrase != NULL ) {
  244.                 show_sentence = SHOW_THAI | SHOW_PHONETIC | SHOW_ENGLISH;
  245.                 scanning = ES_SEARCH;
  246.                 new_sentence ( phrase );
  247.             }
  248.             else
  249.                 not_found ();
  250.             break;
  251.  
  252.         case TW_SEARCH :
  253.             strcpy ( scan.thai , screen_word.thai );
  254.             phrase = thai_search ( &word_head , scan.thai , NEXT_ENTRY );
  255.             if ( phrase != NULL ) {
  256.                 show_word = SHOW_THAI | SHOW_PHONETIC | SHOW_ENGLISH;
  257.                 scanning = TW_SEARCH;
  258.                 new_word ( phrase );
  259.             }
  260.             else
  261.                 not_found ();
  262.             break;
  263.  
  264.         case PW_SEARCH :
  265.             strcpy ( scan.phonetic , screen_word.phonetic );
  266.             phrase = phonetic_search ( &word_head , scan.phonetic ,
  267.                 NEXT_ENTRY );
  268.             if ( phrase != NULL ) {
  269.                 show_word = SHOW_THAI | SHOW_PHONETIC | SHOW_ENGLISH;
  270.                 scanning = PW_SEARCH;
  271.                 new_word ( phrase );
  272.             }
  273.             else
  274.                 not_found ();
  275.             break;
  276.  
  277.         case EW_SEARCH :
  278.             strcpy ( scan.english , screen_word.english );
  279.             phrase = english_search ( &word_head , scan.english , NEXT_ENTRY );
  280.             if ( phrase != NULL ) {
  281.                 show_word = SHOW_THAI | SHOW_PHONETIC | SHOW_ENGLISH;
  282.                 scanning = EW_SEARCH;
  283.                 new_word ( phrase );
  284.             }
  285.             else
  286.                 not_found ();
  287.             break;
  288.  
  289.         case NEXT_ENTRY :
  290.             do_scan ( NEXT_ENTRY );
  291.             break;
  292.  
  293.         case PREV_ENTRY :
  294.             do_scan ( PREV_ENTRY );
  295.             break;
  296.  
  297.         case TS_ENTRY :
  298.             entry = TS_ENTRY;
  299.             /* on_thai_keyboard (); */
  300.             break;
  301.  
  302.         case PS_ENTRY :
  303.             break;
  304.  
  305.         case ES_ENTRY :
  306.             break;
  307.  
  308.         case TW_ENTRY :
  309.             entry = TW_ENTRY;
  310.             /* on_thai_keyboard (); */
  311.             break;
  312.  
  313.         case PW_ENTRY :
  314.             break;
  315.  
  316.         case EW_ENTRY :
  317.             break;
  318.  
  319.         case TRANSLATE_SENTENCE :
  320.             if ( screen_sentence.thai[0] != '\0' )
  321.                 sentence = screen_sentence.thai;
  322.             else
  323.                 sentence = chosen_sentence->thai;
  324.             error_at = split_sentence ( sentence );
  325.             if ( error_at != NULL ) {
  326.                 /* error occured at 'error_at' */
  327.                 sprintf ( body_buf , "Failed at character %d" ,
  328.                     error_at - sentence + 1 );
  329.                 AutoRequest ( window , &body_text , (char*)NULL , &ok_text ,
  330.                     (LONG)0 , (LONG)0 , (LONG)REQ_WIDTH , (LONG)REQ_HEIGHT );
  331.  
  332.             }
  333.             else {
  334.                 screen_sentence.phonetic[0] = '\0';
  335.                 for ( i = 0; i < num_split_words; i++ ) {
  336.                     strcat ( screen_sentence.phonetic , split_words[i]->phonetic );
  337.                 }
  338.                 cur_split_word = -1;
  339.                 scanning = CHOOSE_WORD;
  340.                 show_word = SHOW_THAI | SHOW_ENGLISH | SHOW_PHONETIC;
  341.                 refresh_gadget ( PS_ENTRY );
  342.             }
  343.             break;
  344.  
  345.         case TRANSLATE_WORD :
  346.             if ( screen_word.thai[0] != '\0' )
  347.                 trans = translate ( screen_word.thai );
  348.             else
  349.                 trans = translate ( chosen_word->thai );
  350.             if ( trans != NULL )
  351.                 strcpy ( screen_word.phonetic , trans );
  352.             refresh_gadget ( PW_ENTRY );
  353.             break;
  354.  
  355.         default :
  356.             break;
  357.         }
  358.     }
  359. }
  360.  
  361.  
  362. edit_thai ( str , field , thai_char )
  363. char *str;
  364. int field;
  365. char thai_char;
  366. {
  367.     char *p;
  368.  
  369.     p = str;
  370.     while ( *p != '\0' )
  371.         p++;
  372.     if ( thai_char == TC_BACK_SPACE ) {
  373.         if ( p > str )
  374.             *(p-1) = '\0';
  375.     }
  376.     else if ( thai_char == TC_CLEAR ) {
  377.         *str = '\0';
  378.     }
  379.     else {
  380.         *p++ = thai_char;
  381.         *p = '\0';
  382.     }
  383.     redraw_thai ( field );
  384. }
  385.  
  386.  
  387. not_found ()
  388. {
  389.     strcpy ( body_buf , "Not Found" );
  390.     AutoRequest ( window , &body_text , (char*)NULL , &ok_text ,
  391.         (LONG)0 , (LONG)0 , (LONG)REQ_WIDTH , (LONG)REQ_HEIGHT );
  392. }
  393.